package pl.webapps.alarmme;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class TimeUtils {
    private SharedPreferences mPreferences;
    private static final String PREF_HOUR = "pref_hour";
    private static final String PREF_MINUTE = "pref_minute";
    private static final String LOG_TAG = "AlarmMe.TimeUtils";

    public TimeUtils(SharedPreferences pref) {
        mPreferences = pref;
    }

    public Integer getLastSavedHour() {
        // Log.d(LOG_TAG, String.format("getLastSavedHour(int hour=%d)",
        // mPreferences.getInt(PREF_HOUR, -1)));
        return new Integer(mPreferences.getInt(PREF_HOUR, -1));
    }

    public Integer getLastSavedMinute() {
        // Log.d(LOG_TAG, String.format("getLastSavedMinute(int minute=%d)",
        // mPreferences.getInt(PREF_MINUTE, -1)));
        return new Integer(mPreferences.getInt(PREF_MINUTE, -1));
    }

    public void saveLastAlarmTime(int hour, int minute) {
        // Log.d(LOG_TAG,
        // String.format("saveLastAlarmTime(int hour=%d, int minute=%d)", hour,
        // minute));
        Editor edit = mPreferences.edit();
        edit.putInt(PREF_HOUR, hour);
        edit.putInt(PREF_MINUTE, minute);
        edit.commit();
    }

    public static String formatTime(int hour, int minute, boolean is24format) {
        String hTime = String.valueOf(hour);
        String mTime = String.valueOf(minute);
        if (hTime.length() < 2) {
            hTime = "0".concat(hTime);
        }
        if (mTime.length() < 2) {
            mTime = "0".concat(mTime);
        }
        if (is24format) {
            return hTime + ":" + mTime;
        } else {
            return hTime + ":" + mTime + (hour < 13 ? " AM" : " PM");
        }
    }
}
